#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

/* 
 * the errno is a macro
 * man fopen : 
 * #include <stdio.h>
 *
 * FILE *fopen(const char *pathname, const char *mode);
 * FILE *fdopen(int fd, const char *mode);
 * FILE *freopen(const char *pathname, const char *mode, FILE *stream);
 *
 * compile : gcc errno.c -o errno
 * run : fopen() failed! errno = 2 (fprintf())
 * 	 fopen(): No such file or directory (perror(), strerror())
 *
 * vim /usr/include/asm-generic/errno-base.h  
 * ENOENT 2  No such file or directory 
 *
 * The FILE pointor is stored in heap (malloc). 
 * So every time when we use fopen() got a FILE pointor, we should use fclose (free()) to free it when we do not use it./
 */

int main()
{
	FILE *fp;

	if ((fp = fopen("tmp1", "r")) == NULL) {
		/*
		 * "r" means open text file for reading. but do not created it if it dose not exit.
		 */

		//fprintf(stderr, "fopen() failed! errno = %d\n", errno);
		//perror("fopen()");
		fprintf(stderr, "fopen(): %s\n", strerror(errno));
		exit(1);
	}
	puts("OK!\n");

	fclose(fp);

	exit(0);
}
